package com.bao.controller;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: bao
 * Date: 2020/05/26 21:25
 * Description:
 * Version: V1.0
 */
@Controller
public class DownLoadController {

    @RequestMapping(value = "/dowload.do",method = RequestMethod.GET)
    public void zyy08(String fileName, HttpServletResponse response, HttpServletRequest request) throws IOException {
        //设置响应流中文件进行下载
        response.setHeader("Content-Disposition","attachment;filename="+fileName);
        //把二进制流放入到响应体中.
        ServletOutputStream os = response.getOutputStream();
        String path = request.getServletContext().getRealPath("files");
        File file = new File(path, fileName);
        byte[] bytes = FileUtils.readFileToByteArray(file);
        os.write(bytes);
        os.flush();
        os.close();
    }

}
